<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教师名字" prop="teacherName">
        <el-select v-model="queryParams.teacherName" clearable placeholder="请选择教师名字">
          <el-option
            v-for="teacherManagement in teacherManagementList"
            :key="teacherManagement.name"
            :label="teacherManagement.name"
            :value="teacherManagement.name"
            @keyup.enter.native="handleQuery">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="考核月数" prop="assessMonth">
        <el-date-picker clearable
          v-model="queryParams.assessMonth"
          type="month"
          value-format="yyyy-MM"
          placeholder="请选择考核月份">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditState">
<!--        <el-input-->
<!--          v-model="queryParams.auditState"-->
<!--          placeholder="请输入审核状态"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.auditState" clearable>
          <el-option
            v-for="dict in dict.type.sys_performance_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @keyup.enter.native="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addPerformance"
          v-hasPermi="['performance:selfAssessment:generatePerformance']"
        >生成本月绩效</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="changeState"
          v-hasPermi="['performance:selfAssessment:change']"
        >完成自评</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="auditPass"
          v-hasPermi="['performance:selfAssessment:auditPass']"
        >完成审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['performance:selfAssessment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['performance:selfAssessment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['performance:selfAssessment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!--列表展示-->
    <el-table v-loading="loading" :data="selfAssessmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="教师名字" align="center" prop="teacherName" />
      <el-table-column label="考核月份" align="center" prop="assessMonth" width="180">
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.assessMonth,'{y}-{m}') }}</span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="目标" align="center" prop="goal" />
      <el-table-column label="权重" align="center" prop="weight" />
      <el-table-column label="自评得分" align="center" prop="selfAssessmentScore" >
      </el-table-column>
      <el-table-column label="自评备注" align="center" prop="selfAssessmentNote">
<!--        <template slot-scope="scope">-->
<!--          <el-input v-model="scope.row.selfAssessmentNote" style="width:50%"></el-input>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditName" />
      <el-table-column label="审核分数" align="center" prop="auditScore">
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="auditNote">
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditState"  >
        <template scope="scope">
          <span v-if="scope.row.auditState==3" style="color: blue">已审核</span>
          <span v-if="scope.row.auditState==2" style="color: red">已自评</span>
          <span v-if="scope.row.auditState==1" style="color: green">待自评</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['performance:selfAssessment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['performance:selfAssessment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改考核自评对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="教师名字" prop="teacherName">
        <el-select v-model="form.teacherName" placeholder="请选择教师名字">
          <el-option
            v-for="teacherManagement in teacherManagementList"
            :key="teacherManagement.name"
            :label="teacherManagement.name"
            :value="teacherManagement.name">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="考核月份" prop="assessMonth">
          <el-date-picker clearable
            v-model="form.assessMonth"
            type="month"
            value-format="yyyy-MM"
            placeholder="请选择考核月数">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-select v-model="form.projectName" placeholder="请输入项目名称" >
            <el-option
              v-for="assess in assessList"
              :key="assess.projectName"
              :label="assess.projectName"
              :value="assess.projectName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目标值" prop="goal">
<!--          <el-input v-model="form.goal" placeholder="请输入目标值" />-->
          <el-select v-model="form.goal" placeholder="请选择目标值名称" >
            <el-option
              v-for="item in assessList"
              :key="item.goal"
              :label="item.goal"
              :value="item.goal">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自评得分" prop="selfAssessmentScore">
          <el-input v-model="form.selfAssessmentScore" placeholder="请输入自评得分" />
        </el-form-item>
        <el-form-item label="自评备注" prop="selfAssessmentNote">
          <el-input v-model="form.selfAssessmentNote" type="textarea" placeholder="请输入自评备注" />
        </el-form-item>
        <el-form-item label="审核分数" prop="auditScore">
          <el-input v-model="form.auditScore" placeholder="请输入审核分数" />
        </el-form-item>
        <el-form-item label="审核备注" prop="auditNote">
          <el-input v-model="form.auditNote" placeholder="请输入审核备注" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSelfAssessment,
  getSelfAssessment,
  delSelfAssessment,
  addSelfAssessment,
  updateSelfAssessment,
  changeAssessState, auditPass, addPerformance
} from "@/api/performance/selfAssessment";
import {listAssess} from "@/api/performance/assess";
import {listTeacherManagement} from "@/api/teacher/teacherManagement";
import {listPerformanceTemplate} from "@/api/performance/performanceTemplate";

export default {
  name: "SelfAssessment",
  dicts: ['sys_performance_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考核自评表格数据
      selfAssessmentList: [],
      teacherManagementList:[],
      assessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        assessMonth: null,
        projectName: null,
        goal: null,
        selfAssessmentScore: null,
        selfAssessmentNote: null,
        auditScore:null,
        auditNote:null,
        auditName: null,
        auditState: null,
        weight: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTeacherList();
    this.getAssessList();
  },
  methods: {
    // /** 根据项目名称查目标值 */
    // getGoal(){
    //   const projectName = this.form.projectName;
    //   console.log(projectName);
    //   var result = getProjectGoal(projectName)
    //   console.log(result);
    //
    // },
    /** 生成本月绩效*/
    addPerformance(){
        this.$modal.confirm('是否确认生成本月绩效"').then(function (){
        return addPerformance();
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("绩效生成成功");
      }).catch(() => {});
    },
    /** 完成审核 */
    auditPass(){
      const ids = this.ids;
      this.$modal.confirm('是否确认完成审核"' + ids + '"的数据项？').then(function() {
        return auditPass(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("审核成功");
      }).catch(() => {});
    },
    /** 完成自评 */
    changeState(row){
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认完成自评"' + ids + '"的数据项？').then(function() {
        return changeAssessState(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("自评成功");
      }).catch(() => {});
    },
    /** 查询绩效管理列表 */
    getAssessList() {
      listAssess(this.queryParams).then(response => {
        this.assessList = response.row
        this.total = response.total;
      });
    },
    /** 查询教师管理列表 */
    getTeacherList() {
      this.loading = true;
      listTeacherManagement(this.queryParams).then(response => {
        this.teacherManagementList = response.rows;
        this.total = response.total;
      });
    },
    /** 查询考核自评列表 */
    getList() {
      this.loading = true;
      listSelfAssessment(this.queryParams).then(response => {
        this.selfAssessmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherName: null,
        assessMonth: null,
        projectName: null,
        goal: null,
        selfAssessmentScore: null,
        selfAssessmentNote: null,
        auditScore:null,
        auditNote:null,
        auditName: null,
        auditState: null,
        weight: null
      };
      this.resetForm("form");
    },
    addMonthPerformance(){

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考核自评";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSelfAssessment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考核自评";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSelfAssessment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSelfAssessment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除考核自评编号为"' + ids + '"的数据项？').then(function() {
        return delSelfAssessment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('performance/selfAssessment/export', {
        ...this.queryParams
      }, `selfAssessment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
